class ComicSweeper < ActionController::Caching::Sweeper
  observe Comic
  
  def before_save(comic)
    @save_rank = comic.rank
	@prev = comic.prev
	@next = comic.next
  end
  
  def after_save(comic)
    expire_cache_for(comic)
	if comic.rank != @save_rank
	  expire_cache_for(@prev)
	  expire_cache_for(@next)
	  expire_cache_for(comic.prev)
	  expire_cache_for(comic.next)
	end
	expire_index
	expire_bookmarks(comic)
  end
  def after_destroy(comic)
    expire_cache_for(comic)
	expire_cache_for(@prev)
	expire_cache_for(@next)
	expire_index
	expire_bookmarks(comic)
  end 

private
  def expire_bookmarks(record)
    expire_page(:controller=>'public', :action=>'bookmark', :v=>record.volume_id)
	expire_page(:controller=>'public', :action=>'bookmark', :c=>record.chapter_id)
	expire_page(:controller=>'public', :action=>'bookmark', :v=>record.story_id)
  end
  def expire_index
    expire_page(:controller=>'public', :action=>'index')
  end
  def expire_cache_for(record)
    if record      
	  expire_page(:controller=>'public', :action=>'webcomic', :id=>record.id) 
	end	 
  end  
  
end